package com.senyin.utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Objects;
import java.util.Properties;

public class JDBCUtil {
    private static Properties properties = null;
    static {
        properties = new Properties();
        try {
            //绝对路径，用相对路径tomcat找不到url
            properties.load(new FileInputStream("F:\\ajavastudy\\javacode\\airui\\personnelManagement\\personnel\\src\\com\\senyin\\utils\\jdbc.properties"));
            Class.forName(properties.getProperty("jdbc.driver"));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConn() throws SQLException {
        return DriverManager.getConnection(properties.getProperty("jdbc.url"),properties.getProperty("jdbc.username"),properties.getProperty("jdbc.password"));
    }

    public static void close(Connection connection, Statement statement, ResultSet resultSet){
        if (!Objects.isNull(statement)){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (!Objects.isNull(connection)){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (!Objects.isNull(resultSet)){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws SQLException {
        Connection conn = getConn();
        System.out.println(conn);
    }
}
